int multiplex_statistical(unsigned char *c, const int c_size, const unsigned char *a, const int a_len, const unsigned char *b, const int b_len) {
    if (c_size < 8 + a_len + b_len) return -1;
    for (int i = 0; i < 4; i++) c[i] = (a_len >> (8 * i)) & 0xFF;
    for (int i = 0; i < 4; i++) c[4 + i] = (b_len >> (8 * i)) & 0xFF;
    int j = 8;
    int min_len = a_len < b_len ? a_len : b_len;
    for (int i = 0; i < min_len; i++) {
        c[j++] = a[i] ? 0x01 : 0x00;
        c[j++] = b[i] ? 0x03 : 0x02;
    }
    if (a_len > b_len) {
        for (int i = min_len; i < a_len; i++) c[j++] = a[i] ? 0x01 : 0x00;
    } else if (b_len > a_len) {
        for (int i = min_len; i < b_len; i++) c[j++] = b[i] ? 0x03 : 0x02;
    }
    return 0;
}

int demultiplex_statistical(unsigned char *a, const int a_size, unsigned char *b, const int b_size, const unsigned char *c, const int c_len) {
    int a_len = 0, b_len = 0;
    for (int i = 0; i < 4; i++) a_len |= (c[i] << (8 * i));
    for (int i = 0; i < 4; i++) b_len |= (c[4 + i] << (8 * i));
    int count_a = 0, count_b = 0;
    int index = 8;
    while (count_a < a_len && count_b < b_len && index < c_len) {
        unsigned char byte = c[index++];
        if (byte == 0x00 || byte == 0x01) {
            if (count_a < a_size) a[count_a++] = byte == 0x01 ? 1 : 0;
        } else if (byte == 0x02 || byte == 0x03) {
            if (count_b < b_size) b[count_b++] = byte == 0x03 ? 1 : 0;
        }
    }
    while (count_a < a_len && index < c_len) {
        unsigned char byte = c[index++];
        if (byte == 0x00 || byte == 0x01) {
            if (count_a < a_size) a[count_a++] = byte == 0x01 ? 1 : 0;
        }
    }
    while (count_b < b_len && index < c_len) {
        unsigned char byte = c[index++];
        if (byte == 0x02 || byte == 0x03) {
            if (count_b < b_size) b[count_b++] = byte == 0x03 ? 1 : 0;
        }
    }
    return 0;
}